-------------------------------------------------------------------------------
--
-------------------------------------------------------------------------------
require("luarocks.require")
require("luasql.mysql")

dofile("common.lua")

--local smtpConfFileName = "./conf/serverSMTP.conf"
--local mysqlConfFileName = "./conf/serverMYSQL.conf"
--local recaptchaConfFileName = "./conf/recaptcha.conf"
--local webConfFileName = "./conf/serverWEB.conf"

-------------------------------------------------------------------------------
-- Get IP from String
-------------------------------------------------------------------------------
function getIpFromString(ipString)
	if (#ipString > 8) then	
		local startPosition, endPosition = string.find(ipString, "&")
		if (#string.sub(ipString,startPosition+1) <= 8) then
			return string.sub(ipString,1,startPosition-1), "*"
		else
			return string.sub(ipString,1,startPosition-1), string.sub(ipString,startPosition+1)
		end
--	else
--		return "het roi", "*"
	end
end


-------------------------------------------------------------------------------
-- Register one session into the database
-------------------------------------------------------------------------------
function insertCurSessionToDb(sessionID, userID, IPMP)
	local mysqlParameters = getMysqlParametersFromConfFile()
	local env = assert (luasql.mysql())
	local con = assert (env:connect(mysqlParameters.database, mysqlParameters.user, mysqlParameters.password, mysqlParameters.hostID, mysqlParameters.port))
	local res = assert (con:execute(string.format([[INSERT INTO session (sessionID, userID, IPMP, timeStart) VALUES('%d', '%d', '%s', '%s')]], tonumber(sessionID), tonumber(userID), tostring(IPMP), tostring(os.time()))))
	con:close()
	env:close()
end

-------------------------------------------------------------------------------
-- Get IPMP & userID from String
-------------------------------------------------------------------------------
function getIPMPUserIDFromString(str) 
	local _, numOfPoint = string.gsub(str, "%p","%p")
	if (numOfPoint == 4) then
		local IPMP, userID = string.match(str, "(.*)%p(.*)")
		return IPMP, userID
	else
		return getDomainNameServerWeb(), 123456789
	end
end

-------------------------------------------------------------------------------
-- Get tokenID et timeCreate from message
-------------------------------------------------------------------------------
function getTokenIDTimeCreate(message)
	local tokenID, timeCreate = getIpFromString(message)
	return tokenID, timeCreate
end

-------------------------------------------------------------------------------
-- Create New Token
-------------------------------------------------------------------------------
function createNewToken()
	userID = cgilua.QUERY.userID
	password = cgilua.QUERY.password
	IPMP = cgilua.QUERY.IPMP
	socket = require("socket")
	print(socket._VERSION)
	local conn = socket.tcp()
	conn:connect(tostring(IPMP), getParameter("SPLAYD_PORT"))
	conn:send("CREATETOKEN\n") -- Message Code
	conn:send(tostring(userID).."\n")
	cgilua.put("alert('Da connect duoc voi database... distrinet')")
	local tokenID = conn:receive("*line")

end
